﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class UIRank : MonoBehaviour 
{
    private Button btn_Bg;
    private Image img_Bg;
    private Color img_BgColor;
    private Transform image;
    private Text text_BestScore;
    private Text text_SecondScore;
    private Text text_ThirdScore;

    private void Awake()
    {
        Init();
    }

    /// <summary>
    /// 初始化UIRank
    /// </summary>
    private void Init()
    {
        EventCenter.AddListener(CL.EventType.ShowUIRank, ShowUIRank);
        EventCenter.AddListener(CL.EventType.HideUIRank, HideUIRank);
        EventCenter.AddListener(CL.EventType.UpdateUIRankUI, UpdateUIRankUI);


        btn_Bg = transform.Find("Btn_Bg").GetComponent<Button>();
        btn_Bg.onClick.AddListener(Btn_Bg);
        img_Bg = transform.Find("Btn_Bg").GetComponent<Image>();
        img_BgColor = img_Bg.color;       
        image = transform.Find("Image");
        image.localScale = Vector3.one;
        text_BestScore = transform.Find("Image/Best/Text").GetComponent<Text>();
        text_SecondScore = transform.Find("Image/Second/Text").GetComponent<Text>();
        text_ThirdScore = transform.Find("Image/Third/Text").GetComponent<Text>();


        gameObject.SetActive(false);// DOTween动画在UIMainOpen第一次执行时露馅了。。。
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener(CL.EventType.ShowUIRank, ShowUIRank);
        EventCenter.RemoveListener(CL.EventType.HideUIRank, HideUIRank);
        EventCenter.RemoveListener(CL.EventType.UpdateUIRankUI, UpdateUIRankUI);
    }


    /// <summary>
    /// 显示、隐藏UIRank界面
    /// </summary>
    private void ShowUIRank()
    {
        gameObject.SetActive(true);       
        img_BgColor = Color.clear;
        img_Bg.DOColor(img_BgColor, 0.3f);
        image.localScale = Vector3.zero;
        image.DOScale(Vector3.one, 0.3f);
    }
    private void HideUIRank()
    {
        img_Bg.DOColor(Color.clear, 0.3f);
        image.DOScale(Vector3.zero, 0.3f).OnComplete(() => { gameObject.SetActive(false); });
        //测试发现下面这行print代码可不会等它Complete后执行
        //print("xxxxxxxxxxcxcxcxcxccccccccccccccc");
    }
   

    /// <summary>
    /// 背景按钮点击（关闭UIRank）
    /// </summary>
    private void Btn_Bg()
    {
        GameManager.Instance.UIRankClose();        
    }


    /// <summary>
    /// 此界面允许一次性更新所有UI显示
    /// </summary>
    private void UpdateUIRankUI()
    {
        text_BestScore.text = DataManager.Instance.mUserData.bestScore[0].ToString();
        text_SecondScore.text = DataManager.Instance.mUserData.bestScore[1].ToString();
        text_ThirdScore.text = DataManager.Instance.mUserData.bestScore[2].ToString();
    }
}
